<?php

namespace app\admin\controller;
use think\Db;
use think\Controller;
use app\common\controller\AdminBaseController;
/**
 * 联盟商家
 */
class Union extends AdminBaseController
{
    /**
     * 联盟商家列表
     * @author [longx] 
     */
    public function index( $page = 1){
    	//获取联盟信息
    	$union_list = db('union_agent')
    					->alias('a')
    					->field('a.*,b.user_name,b.mobile')
						->join('info_user b','a.u_id = b.id')
    					->paginate(20);
        return $this->fetch('index', ['list' => $union_list]);
    }
    /**
     * 查询
     * @author [longx] 
     */
    public function search(){
     $map=[];
           
       if(request()->isPost()){
		 	$param = input('post.');
          
			//联盟名称
			if($param['name']){
				$map['name'] = ['like', '%'.$param['name'].'%'];
			}
			//审核状态
			if($param['status']){
				$map['status'] = ['like', '%'.$param['status'].'%'];
			}
			//负责人名称
			if($param['shopowner']){
				$map['shopowner'] = ['like', '%'.$param['shopowner'].'%'];
			}
		    //创建时间
			if($param['createtime']&&$param['end_time']){
				$map['createtime'] = ['between time', [$param['createtime'],$param['end_time']]];
			}
			
			
  	    $list=Db::name("mall_unionstore")
       ->where($map)
       ->paginate(10);

		
        return $this->fetch('index', ['list' => $list]);
      }
    }


     /**
      * 冻结
      */
       public function frzee($id,$state){
       	 $id = input('param.id');
       	 $param['state'] = input('param.state');

       	 if($param['state'] == '1'){
       	 	$param['state']='2';
       	 }else{
       	 	$param['state']='1';
       	 }
       	 Db::table('long_mall_unionstore')->where('id',$id)->update(['state'=> $param['state']]);
       	 $list=Db::name("mall_unionstore")->paginate(10);
  		return $this->fetch('index',['list'=>$list]);
       }
      
      
      /**
       * 查看联盟成员
       */
      public function show($id){
      	$list=Db::name('mall_unionitem')->alias('a')
   		->join('mall_agent b','a.agent_id=b.id','LEFT')
   		->field('b.score,b.ad_money ,b.store_tel,b.store_name')
   		->where('a.union_id',input('param.id'))->select();

       return $this->fetch('',['list'=> $list]);
    }
      
    /**
     * [edit 审核]
     * @return [type] [description]
     * @author [longx]
     */
    public function edit($id)
    {
        if(request()->isAjax()){
            $param = input('post.');  
			$param['audittime'] = now_time();       
			//审核通过状态为2
			$param['status'] = '2';
			if ($this->model->allowField(true)->save($param,$id) !== false) {
                    $this->success('审核成功', url('union_store/index'));
            } else {
                $this->error('审核失败');
            }
        }
        $id = input('param.id');
        $this->assign('ad',$this->model->find($id));
        return $this->fetch();
    }



    /**
     * [del删除]
     * @author [longx] 
     */
    public function delete()
    {
        $id = input('param.id');
        if ($this->model->destroy($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
     /**
     * [eexamines 审核]
     * @return [type] [description]
     * @author [longx]
     */
    public function examines()
    {
    	$is_examines = input('is_examines');
    	$union_id = input('id');
    	
    	//获取联盟信息
        $union_info = db('union_agent')
				        ->alias('a')
						->join('info_user b','a.u_id = b.id')
        				->where('a.id',$union_id)
        				->find();
    	
    	if(request()->isAjax()){
    		$result = db('union_agent')->where('id', input('union_id'))->update(['state' => input('state')]);
    		if($result){
    			return json(['code'=>200,'msg'=>'操作成功']);
    		}else{
    			return json(['code'=>404,'msg'=>'操作失败']);
    		}
    		
    		
    		
    	}
    	
        $this->assign('union_info',$union_info);
        return $this->fetch();
    }
    /**
    
    /**
     * [ 审核通过]
     * @return [type] [description]
     * @author [longx]
     */
    public function pass($id){
    	if(request()->isAjax()){
            $param = input('post.');
            $param['audittime'] = now_time();       
			//审核通过状态为2
			$result = $this->model->allowField(true)->save($param,$id);
         	if($result && $param['status'] ==2) {
         		//修改联盟详情状态为已审核
         		$res = db('mall_unionitem')->where('union_id',$id)->update(['apply' => 2]);
				
				//获取联盟信息
				$union_info = db('mall_unionstore')->where('id',$id)->find();
					
				$agent_info = db('mall_agent')->where('id',$union_info['agent_id'])->find();
				if($agent_info['is_round'] !=2){
					//修改店铺状态为联盟
					$res2 = db('mall_agent')->where('id',$union_info['agent_id'])->update(['is_union' => 2,'is_round'=>2]);
					if(!$res2){
						$this->error('修改店铺信息失败');
					}
				}
				$this->success('修改成功', url('Union_store/index'));
				
            } else {
                $this->error('修改失败');
            }
        }
        $id = input('param.id');
        $this->assign('ad',$this->model->find($id));
        return $this->fetch();
    }
}
